package com.yunzhou.action;

import com.alibaba.fastjson.JSONObject;
import com.yunzhou.common.DataChangeService;
import com.yunzhou.common.DataChangeServiceImpl;
import com.yunzhou.u8.*;
import java.util.*;

import com.yunzhou.utils.LogTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.common.util.string.StringUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;
import weaver.conn.RecordSet;


/**
 * ZC02-低值易耗品采购申请流程审批完成将数据传入U8系统
 */
public class FlyRiseOKAction implements Action {

	private static Log log = LogFactory.getLog(FlyRiseOKAction.class.getName());
	private LogTool logTool = new LogTool("/log/huangwm/u8/action/FlyRiseOKAction",false);

	@Override
	public String execute(RequestInfo requestInfo) {

		String requestID = requestInfo.getRequestid();
		String workflowid = requestInfo.getWorkflowid();
		String requestname = requestInfo.getRequestManager().getRequestname();
		logTool.writeLog((new StringBuilder()).append("FlyRiseOKAction开始====").append(requestID).append("，workflowid：")
				.append(workflowid).toString());
		String ssgs = "";
		String company = "";
		String liucbh = "";
		String shenqr = "";
		String shenqrq = "";
		String shenqrd = "";
		String shenqbm = "";
		String shenqyy = "";
		String xmmcnew = "";
		String caigxm = "";
		String xiangmdh = "";
		String xmjll = "";
		String xiangmzs = "";
		String xiangmzsmc = "";
		String qwdhq = "";
		String shenhr = "";
		String shenqcg = "";
		MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
		Property[] fields = mainTableInfo.getProperty();
		Property[] arr = fields;
		int len = arr.length;
		for (int i = 0; i < len; i++) {
			Property property = arr[i];
			String name = property.getName();
			String value = property.getValue();
			if (name.equals("fygsgs")) {
				ssgs = value;
			}
		}

		mainTableInfo = requestInfo.getMainTableInfo();
		fields = mainTableInfo.getProperty();
		arr = fields;
		len = arr.length;
		for (int i = 0; i < len; i++) {
			Property property = arr[i];
			String name = property.getName();
			String value = property.getValue();
			if (name.equals("fygsgs")) {
				company = value;
			}
			if (name.equals("liucbh")) {
				liucbh = value;
			}
			if (name.equals("shenqr")) {
				shenqr = value;
			}
			if (name.equals("shenqrq")) {
				shenqrq = value;
			}
			if (name.equals("shenqrd")) {
				shenqrd = value;
			}
			if (name.equals("shenqbm")) {
				shenqbm = value;
			}
			if (name.equals("shenqyy")) {
				shenqyy = value;
			}
			if (name.equals("xmnew")) {
				xmmcnew = value;
			}
			if (name.equals("caigxm")) {
				caigxm = value;
			}
			if (name.equals("xiangmdh")) {
				xiangmdh = value;
			}
			if (name.equals("xmjll")) {
				xmjll = value;
			}
			if (name.equals("xiangmzs")) {
				xiangmzs = value;
			}
			if (name.equals("qwdh")) {
				qwdhq = value;
			}
			if (name.equals("shr")) {
				shenhr = value;
			}
			if (name.equals("shenqcg")) {
				shenqcg = value;
			}
		}
		//固资类不传U8
		if (shenqcg.equals("1")) {
			return Action.SUCCESS;
		}
		//
		if (company.equals("19")) {
			return Action.SUCCESS;
		}
		// 查询资产管理员审批节点的userID
		RecordSet rs = new RecordSet();
		String sql2 = "select top 1 userid from workflow_currentoperator with(nolock) \r\n" + "		 where requestid="
				+ requestID + " and nodeid in (6809,7289,6679,8860,9162,10044,10399) and isremark=2";
		rs.execute(sql2);
		if (rs.next()) {
			shenhr = rs.getString("userid");
		}

		logTool.writeLog((new StringBuilder()).append("转换前公司 （ssgs、company）: ").append(ssgs).append(",").append(company)
				.toString());
		logTool.writeLog((new StringBuilder()).append("主表信息 - requestname:").append(requestname).append(",caigxm:")
				.append(caigxm).append(",liucbh:").append(liucbh).append(",qwdhq:").append(qwdhq).append(",shenqr:")
				.append(shenqr).append(",shenqrq:").append(shenqrq).append(",shenqyy:").append(shenqyy)
				.append(",shenqr:").append(shenqr).append(",xiangmzs:").append(xiangmzs).append(",xmjll:").append(xmjll)
				.append(",xmmcnew:").append(xmmcnew).toString());
		DataChangeService dataChangeService = new DataChangeServiceImpl();
		HashMap<String, String> ryMap = dataChangeService.getSpecifiedTableMap("hrmresource", "id", "workcode");
		if (StringUtil.isNotNullAndEmpty(xiangmzs) && xiangmzs != "null") {
			String[] as = xiangmzs.split(",");
			for (int k = 0; k < as.length; k++) {
				if (xiangmzsmc.equals("")) {
					xiangmzsmc = ryMap.get(as[k]).toString();
				} else {
					xiangmzsmc = (new StringBuilder()).append(xiangmzsmc).append(",").append(ryMap.get(as[k]))
							.toString();
				}
			}

		}
		HashMap<String, String> mapGs = dataChangeService.getSpecifiedTableMap("hrmsubcompany", "id", "subcompanycode");
		xmmcnew = dataChangeService.getSingleBrowseValue("xmmc", "uf_xmhdb", "id", xmmcnew);
		logTool.writeLog((new StringBuilder()).append("传入U8主表信息 - requestname:").append(requestname).append(",caigxm:")
				.append(caigxm).append(",liucbh:").append(liucbh).append(",qwdhq:").append(qwdhq)
				.append(",ryMap.get(shenqr).toString():").append(shenqr.isEmpty() ? "" : ryMap.get(shenqr).toString())
				.append(",shenqrq:").append(shenqrq).append(",shenqyy:").append(shenqyy).append(",shenqr:")
				.append(shenqr).append(",xiangmzs:").append(xiangmzs).append(",ryMap.get(xmjll).toString():")
				.append(xmjll.isEmpty() ? "" : ryMap.get(xmjll).toString()).append(",xmmcnew:").append(xmmcnew)
				.toString());
		workflowid = dataChangeService.getPrimevalID(workflowid);
		logTool.writeLog((new StringBuilder()).append("最小版本id : ").append(workflowid).toString());
		PuAppVouch puApp = new PuAppVouch();
		if (workflowid.equals("662")) {
			logTool.writeLog((new StringBuilder()).append("shenhr分别是 : ").append(shenhr).append(",")
					.append(shenhr.isEmpty() ? "" : ryMap.get(shenhr).toString()).toString());
			PuAppVouchHead appVouch = new PuAppVouchHead();
			appVouch.setRequestid(requestID);
			appVouch.setBiaoti(requestname);
			appVouch.setLiucbh(liucbh);
			appVouch.setXmmcnew(xmmcnew);
			appVouch.setQwdhq(qwdhq);
			appVouch.setShenqr(shenqr.isEmpty() ? "" : ryMap.get(shenqr).toString());
			appVouch.setShenqrq(shenqrq);
			appVouch.setShenqyy(shenqyy);
			appVouch.setCcode(liucbh);
			appVouch.setShenhr(shenhr.isEmpty() ? "" : ryMap.get(shenhr).toString());
			logTool.writeLog((new StringBuilder()).append("FlyRiseOKAction-主表信息，Requestid：").append(requestID)
					.append(",Biaoti：").append(requestname).append(",Liucbh：").append(liucbh).append(",Shenqr：")
					.append(shenqr.isEmpty() ? "" : ryMap.get(shenqr).toString()).append(",Shenqrq：").append(shenqrq)
					.append(",Shenqyy：").append(shenqyy).append(",Ccode：").append(liucbh).append(",Shenhr：")
					.append(shenhr.isEmpty() ? "" : ryMap.get(shenhr).toString()).toString());
			List<JSONObject> list = getJsonArray(requestInfo.getDetailTableInfo().getDetailTable(0));
			logTool.writeLog((new StringBuilder()).append("FlyRiseOKAction-list明细信息 : ").append(list).toString());
			int j = list.size();
			logTool.writeLog((new StringBuilder()).append("j : ").append(j).toString());
			logTool.writeLog((new StringBuilder()).append("company == '7' : ").append(company == "7").toString());
			logTool.writeLog((new StringBuilder()).append("company.equals('7'): ").append(company.equals("7")).toString());
			PuAppVouchBody[] appVouchs = new PuAppVouchBody[j];

			//****************************将明细行ID和预算金额传到U8 KangMD 20250324  start****************************//
			//获取流程表名
			String tablename = requestInfo.getRequestManager().getBillTableName();
			int i=0;
			//查询明细行最新数据
			sql2="select * from "+tablename+"_dt1 where mainid in (select id from "+tablename+" where requestid="+requestID+")";
			logTool.writeLog("查询明细行最新数据---------->"+sql2);
			rs.execute(sql2);
			while (rs.next()) {
				String wlbm = Util.null2String(rs.getString("wlbm"));//物料编码
				String shul = Util.null2String(rs.getString("shul"));//shul
				String beizNew = Util.null2String(rs.getString("beizNew"));//备注
				String xqr = Util.null2String(rs.getString("xqr"));//需求人
				String shdz = Util.null2String(rs.getString("shdz"));//收货地址

				String id = Util.null2String(rs.getString("id"));//明细行ID
				String mainid = Util.null2String(rs.getString("mainid"));//主表ID
				String ysje = Util.null2o(rs.getString("ysje"));//预算金额
				String dj = Util.null2o(rs.getString("dj"));//预算单价
				String pkid=requestID+'_'+mainid+"_1_"+id;//明细行主键ID=requesid_主表ID_1_明细行ID

				appVouchs[i] = new PuAppVouchBody();
				appVouchs[i].setOaid(pkid);//OA明细行主键ID
				appVouchs[i].setWlbm(wlbm);
				appVouchs[i].setShul(shul);
				appVouchs[i].setDanj(dj);
				appVouchs[i].setJine(ysje);
				appVouchs[i].setCgy("");
				appVouchs[i].setBeiz(beizNew + "^"+ ryMap.get(xqr));
				logTool.writeLog(beizNew+ "^"+ ryMap.get(xqr));
				appVouchs[i].setShdz(shdz);

				i++;
			}
			//****************************将明细行ID和预算金额传到U8 KangMD 20250324  end****************************//
			/*
			for (int i = 0; i < j; i++) {
				logTool.writeLog((new StringBuilder()).append("i进来了 : ").append(i).append("，company : ").append(company)
						.append("，wlbm : ").append(((JSONObject) list.get(i)).getString("wlbm")).toString());
				appVouchs[i] = new PuAppVouchBody();
				appVouchs[i].setWlbm(((JSONObject) list.get(i)).getString("wlbm"));
				appVouchs[i].setShul(((JSONObject) list.get(i)).getString("shul"));
				appVouchs[i].setDanj("0");
				appVouchs[i].setJine("0");
				appVouchs[i].setCgy("");
				appVouchs[i].setBeiz(((JSONObject) list.get(i)).getString("beizNew") + "^"
						+ ryMap.get(((JSONObject) list.get(i)).getString("xqr")).toString());
				logTool.writeLog(((JSONObject) list.get(i)).getString("beizNew") + "^"
						+ ryMap.get(((JSONObject) list.get(i)).getString("xqr")).toString());
				appVouchs[i].setShdz(((JSONObject) list.get(i)).getString("shdz"));
				// appVouchs[i].setCgy(((JSONObject)list.get(i)).getString("cgy").isEmpty() ? ""
				// : ryMap.get(((JSONObject)list.get(i)).getString("cgy")).toString());
			}
			 */

			logTool.writeLog((new StringBuilder()).append("转换后公司 :").append(mapGs.get(company).toString()).toString());
		    puApp.setCCompany(mapGs.get(company).toString());
			// 测试账套
			//puApp.setCCompany("YZ_test");
			puApp.setCptname("低值易耗品采购");
			puApp.setAppVouch(appVouch);
			puApp.setAppVouchs(appVouchs);
		}
		logTool.writeLog("FlyRiseOKAction开始调用接口====");
		logTool.writeLog(JSONObject.toJSONString(puApp));
		String result = "";
		try {
			ServiceForOA workflowServiceXmlLocator = new ServiceForOALocator();
			result = workflowServiceXmlLocator.getserviceForOASoap12().addPOVouch(puApp);
			logTool.writeLog("ZC02接口返回信息===="+result);
		} catch (Exception ex) {
			logTool.writeLog((new StringBuilder()).append("FlyRiseOKAction输出有值，返回了错误====").append(result).toString());
			requestInfo.getRequestManager().setMessagecontent(result);
			ex.printStackTrace();
		}
		logTool.writeLog((new StringBuilder()).append("result====").append(result).toString());
		logTool.writeLog((new StringBuilder()).append("FlyRiseOKAction结束====").append(requestID).toString());

		if (result.isEmpty()) {
			return Action.SUCCESS;
		} else {
			if (result != "") {
				requestInfo.getRequestManager().setMessagecontent(result);
			}
			return Action.FAILURE_AND_CONTINUE;
		}
	}

	private List<JSONObject> getJsonArray(DetailTable detailTable) {
		List<JSONObject> list = new ArrayList<JSONObject>();
		Row[] arr1 = detailTable.getRow();
		int len1 = arr1.length;
		for (int i1 = 0; i1 < len1; i1++) {
			Row row = arr1[i1];
			JSONObject jsonObject = new JSONObject();
			Cell[] arr = row.getCell();
			int len = arr.length;
			for (int i = 0; i < len; i++) {
				Cell cell = arr[i];
				if ("gsgs".equals(cell.getName())) {
					jsonObject.put("ssfygs", cell.getValue());
					continue;
				}
				if ("wlbm".equals(cell.getName())) {
					jsonObject.put("wlbm", cell.getValue());
					continue;
				}
				if ("yzwlbm".equals(cell.getName())) {
					jsonObject.put("yzwlbm", cell.getValue());
					continue;
				}
				if ("wupmc".equals(cell.getName())) {
					jsonObject.put("wupmc", cell.getValue());
					continue;
				}
				if ("guigxh".equals(cell.getName())) {
					jsonObject.put("guigxh", cell.getValue());
					continue;
				}
				if ("shul".equals(cell.getName())) {
					jsonObject.put("shul", cell.getValue());
					continue;
				}
				if ("danw".equals(cell.getName())) {
					jsonObject.put("danw", cell.getValue());
					continue;
				}
				if ("danj".equals(cell.getName())) {
					jsonObject.put("danj", cell.getValue());
					continue;
				}
				if ("jine".equals(cell.getName())) {
					jsonObject.put("jine", cell.getValue());
					continue;
				}
				if ("cgy".equals(cell.getName())) {
					jsonObject.put("cgy", cell.getValue());
					continue;
				}
				if ("beizNew".equals(cell.getName())) {
					jsonObject.put("beizNew", cell.getValue());
					continue;
				}
				if ("shdz".equals(cell.getName())) {
					jsonObject.put("shdz", cell.getValue());
					continue;
				}
				if ("xqr".equals(cell.getName())) {
					jsonObject.put("xqr", cell.getValue());
					continue;
				}
				if ("ywlx".equals(cell.getName()))
					jsonObject.put("ywlx", cell.getValue());
			}

			list.add(jsonObject);
		}

		return list;
	}

}
